<?php

class Purchasing extends CI_Model
{

    var $user_id = '';
    var $product_id = '';
    var $purchasing_time = '';
    var $purchasing_price = 0;

    function __construct()
    {
        parent::__construct();
        date_default_timezone_set('Asia/Bangkok');
    }

    function get_last_ten_entries()
    {
        $query = $this->db->get('User', 10);
        return $query->result();
    }

    function count_profit()
    {
        $this->db->select_sum('purchasing_price');
        $this->db->from('purchasing');
        $query = $this->db->get();
        return $query->result()[0]->purchasing_price;
    }

    function get_month_profit($month, $year)
    {
        $query = $this->db->query("select sum(purchasing_price)
                        from purchasing
                        where purchasing.purchasing_time is not null
                        and month(FROM_UNIXTIME(purchasing_time)) = $month and year(FROM_UNIXTIME(purchasing_time)) = $year");
        return $query->result_array()[0];
    }

    function count_today_profit()
    {
        $start = strtotime('today midnight');
        $end = strtotime('tomorrow midnight');

        $query = $this->db->query("select sum(purchasing_price) as sum from purchasing where purchasing_time < $end and purchasing_time > $start");
        $result = $query->result();
        if ($result[0]->sum) {
            return $result[0]->sum;
        } else return 0;
    }

    function count_month_profit()
    {
        $start = strtotime('first day of this month midnight');
        $end = strtotime('first day of next month midnight');

        $query = $this->db->query("select sum(purchasing_price) as sum from purchasing where purchasing_time < $end and purchasing_time > $start");
        $result = $query->result();
        if ($result[0]->sum) {
            return $result[0]->sum;
        } else return 0;
    }

    function count_year_profit()
    {
        $start = strtotime('first day of January this year');
        $end = strtotime('first day of January next year');

        $query = $this->db->query("select sum(purchasing_price) as sum from purchasing where purchasing_time < $end and purchasing_time > $start");
        $result = $query->result();
        if ($result[0]->sum) {
            return $result[0]->sum;
        } else return 0;
    }

    function save()
    {
        $this->user_id = $this->input->get('user_id');
        $this->product_id = $this->input->get('product_id');
        $this->purchasing_time = $this->input->get('purchasing_time');
        $this->db->insert('purchasing', $this);
    }

    function update()
    {
        $this->user_id = $this->input->get('user_id');
        $this->product_id = $this->input->get('product_id');
        $this->purchasing_time = $this->input->get('purchasing_time');
        $this->db->update('purchasing', $this, array('id' => $this->input->post('id')));
    }

    function add_cart($pid)
    {
        $session = $this->session->userdata('logged_in');
        $this->user_id = $session['id'];
        $this->product_id = $pid;
        $this->purchasing_time = null;

        $product = $this->product->get($pid)[0];
        $promo = $this->product->get_promotion($pid);

        $this->purchasing_price = $product->price;

        if ($promo && $promo->start <= time() && $promo->end > time()) {
            $this->purchasing_price = round($product->price - $promo->discount / 100 * $product->price, 1);
        }

        $this->db->insert('purchasing', $this, array('id' => $this->input->post('id')));
        return 1;
    }

    function purchased($uid, $pid)
    { // Kiểm tra đã thanh toán chưa, đã bỏ vào cart chưa

        $this->db->select('product_id,purchasing_time');
        $this->db->from('purchasing');
        $this->db->where('user_id', $uid);
        $this->db->where('product_id', $pid);
        $products = $this->db->get()->result();

        if (count($products) == 0) return 0;
        else {
            if ($products[0]->purchasing_time == null)
                return 1; // Trong giỏ hàng
            return 2; // Đã thanh toán
        }
    }

    function get_all_purchasing($uid)
    {
        $this->db->select('product_id as id');
        $this->db->from('purchasing');
        $this->db->where('user_id', $uid);
        $this->db->where('purchasing_time', null);
        $ids = $this->db->get()->result();

        $product = array();

        foreach ($ids as $id) {
            array_push($product, $this->product->get($id->id)[0]);
        }

        return $product;

    }

    function remove($uid, $pid)
    {
        $this->db->delete('purchasing', array('product_id' => $pid, 'user_id' => $uid));
        return 1;
    }

    function set_purchased($uid, $pid)
    {
        $this->user_id = $uid;
        $this->product_id = $pid;
        $this->purchasing_time = time();

        $product = $this->product->get($pid)[0];
        $promo = $this->product->get_promotion($pid);

        $this->purchasing_price = $product->price;

        if ($promo && $promo->start <= time() && $promo->end > time()) {
            $this->purchasing_price = round($product->price - $promo->discount / 100 * $product->price, 1);
        }

        $this->db->update('purchasing', $this, array('user_id' => $uid, 'product_id' => $pid));
    }

    function check_user_purchased_product($uid, $pid)
    {
        $this->db->select('purchasing_time');
        $this->db->from('purchasing');
        $this->db->where('user_id', $uid);
        $this->db->where('product_id', $pid);
        $result = $this->db->get()->result();

        return (count($result) > 0) ? $result[0]->purchasing_time != null : false;
    }

}